package com.shenjinzheng.apply.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.shenjinzheng.apply.dto.MerchantApplyDTO;
import com.shenjinzheng.apply.entity.MerchantApply;
import com.shenjinzheng.apply.mapper.MerchantApplyMapper;
import com.shenjinzheng.apply.service.MerchantApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenjinzheng.merchant.MerchantClient;
import com.shenjinzheng.merchant.entity.MerchantInfo;
import com.shenjinzheng.sso.SSOClient;
import com.shenjinzheng.sso.entity.Merchant;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商家入驻申请表 服务实现类
 * </p>
 *
 * @author shenjinzheng
 * @since 2024-11-10
 */
@Slf4j
@Service
public class MerchantApplyServiceImpl extends ServiceImpl<MerchantApplyMapper, MerchantApply> implements MerchantApplyService {

    @Resource
    private HttpServletRequest request;

    @Resource
    private SSOClient ssoClient;

    @Resource
    private MerchantClient merchantClient;

    /**
     * 申请
     *
     * @param merchantApply
     */
    @Override
    public void apply(MerchantApply merchantApply) {
        String token = request.getHeader("Authorization");
        log.info("token:{}", token);
        String memberId = ssoClient.getMemberIdByToken();
        log.info("memberId:{}", memberId);
        merchantApply.setMemberId(memberId);
        this.save(merchantApply);
    }

    /**
     * 批准
     *
     * @param merchantApplyDTO
     */
    @Override
    public void approval(MerchantApplyDTO merchantApplyDTO) {
        //1.1 根据Id获取申请单信息
        MerchantApply merchantApply = this.getById(merchantApplyDTO.getId());
        //1.2 判断是否待审核,如果不是,抛异常
        // (比如状态为 1-通过,则说明订单已经审批过,抛异常,保证业务幂等性)
        if (!"0".equals(merchantApply.getStatus())) {
            throw new RuntimeException("申请单状态异常");
        }
        //1.3 状态为待审核,则改为前端传过来的状态(通过或未通过)
        //1.4 如果未通过,还需说明原因
        merchantApply.setStatus(merchantApplyDTO.getStatus());
        merchantApply.setRemark(merchantApplyDTO.getRemark());
        this.updateById(merchantApply);
        //申请不通过则不需要做其他事情
        if (!"1".equals(merchantApplyDTO.getStatus())){
            return;
        }
        //申请通过后,还需要生成商户信息
        //2.1 生成商户登录相关信息 sso微服务
        Merchant merchant = new Merchant();
        merchant.setId(merchantApply.getMemberId());
        merchant.setPhone(merchantApply.getPhone());
        //密码设为身份证后六位
        merchant.setPassword(merchantApply.getIdCard().substring(merchantApply.getIdCard().length() - 6));
        ssoClient.save(merchant);
        //2.2 生成商户非登录相关信息 merchant微服务
        //2.2.1 拷贝申请单中相同字段信息到商户信息表
        MerchantInfo merchantInfo = new MerchantInfo();
        BeanUtil.copyProperties(merchantApply, merchantInfo);
        merchantInfo.setId(merchant.getId());
        merchantInfo.setMerchantApplyId(merchantApply.getId());
        log.info("merchantInfo:{}", merchantInfo);
        merchantClient.saveMerchant(merchantInfo);

    }
}
